# import logging
from Definition import Definition


class MappingDefinition(Definition):
	def __init__(self, file):
		super().__init__(file)

	def map(self, values):
		mappings = self._get_mappings()
		mapped_values = {}
		for value in values:
			self._map(value, mappings, mapped_values)
		return mapped_values

	def _map(self, value, mappings, mapped_values):
		selectable_mappings = [mapping for mapping in mappings if mapping['input'] == value]
		for selectable_mapping in selectable_mappings:
			mapped_values[selectable_mapping['output']['name']] = selectable_mapping['output']['state']

	def _get_mappings(self):
		mappings = self.content.get('mappings')
		if mappings is None:
			raise(Exception('invalid mapping definition, missing mappings'))
		return mappings

	@classmethod
	def read(cls):
		return cls.read_folder('mapping')
